package cn.jeeweb.web.modules.equipment.service.impl;

import cn.jeeweb.common.mybatis.mvc.service.impl.CommonServiceImpl;
import cn.jeeweb.common.mybatis.mvc.wrapper.EntityWrapper;
import cn.jeeweb.common.utils.StringUtils;
import cn.jeeweb.web.modules.equipment.entity.EqsMaintenanceCheck;
import cn.jeeweb.web.modules.equipment.entity.EqsMaintenanceTask;
import cn.jeeweb.web.modules.equipment.mapper.EqsMaintenanceCheckMapper;
import cn.jeeweb.web.modules.equipment.service.IEqsMaintenanceCheckService;
import cn.jeeweb.web.modules.equipment.service.IEqsMaintenanceTaskService;
import cn.jeeweb.web.modules.sys.entity.FileResponse;
import cn.jeeweb.web.utils.UserUtils;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @description: eqs_maintenance_check服务接口
*/
@Transactional
@Service("eqsMaintenanceCheckService")
public class EqsMaintenanceCheckServiceImpl extends CommonServiceImpl<EqsMaintenanceCheckMapper,EqsMaintenanceCheck> implements IEqsMaintenanceCheckService {

    @Autowired
    private IEqsMaintenanceTaskService eqsMaintenanceTaskService;

    @Override
    public EqsMaintenanceCheck getEqsMaintenanceCheck(EqsMaintenanceCheck eqsMaintenanceCheck) {
        EqsMaintenanceCheck result = null;
        result = baseMapper.getEqsMaintenanceCheck(eqsMaintenanceCheck);
        if(result != null){
            List<FileResponse> defaultList = new ArrayList<FileResponse>();
            if(StringUtils.isNotBlank(result.getCheckAreaImage())){
                List<FileResponse> responses = JSON.parseArray(result.getCheckAreaImage(),FileResponse.class);
                result.setCheckAreaImageArray(responses);
            }else{
                result.setCheckAreaImageArray(defaultList);
            }

            if(StringUtils.isNotBlank(result.getCheckImportImage())){
                List<FileResponse> responses = JSON.parseArray(result.getCheckImportImage(),FileResponse.class);
                result.setCheckImportImageArray(responses);
            }else{
                result.setCheckImportImageArray(defaultList);
            }

            if(StringUtils.isNotBlank(result.getUseToolFile())){
                List<FileResponse> responses = JSON.parseArray(result.getUseToolFile(),FileResponse.class);
                result.setUseToolFileArray(responses);
            }else{
                result.setUseToolFileArray(defaultList);
            }

            if(StringUtils.isNotBlank(result.getCheckStandardFile())){
                List<FileResponse> responses = JSON.parseArray(result.getCheckStandardFile(),FileResponse.class);
                result.setCheckStandardFileArray(responses);
            }else{
                result.setCheckStandardFileArray(defaultList);
            }

            if(StringUtils.isNotBlank(result.getCheckMethodFile())){
                List<FileResponse> responses = JSON.parseArray(result.getCheckMethodFile(),FileResponse.class);
                result.setCheckMethodFileArray(responses);
            }else{
                result.setCheckMethodFileArray(defaultList);
            }

            if(StringUtils.isNotBlank(result.getExistRiskFile())){
                List<FileResponse> responses = JSON.parseArray(result.getExistRiskFile(),FileResponse.class);
                result.setExistRiskFileArray(responses);
            }else{
                result.setExistRiskFileArray(defaultList);
            }

            if(StringUtils.isNotBlank(result.getCountermeasuresFile())){
                List<FileResponse> responses = JSON.parseArray(result.getCountermeasuresFile(),FileResponse.class);
                result.setCountermeasuresFileArray(responses);
            }else{
                result.setCountermeasuresFileArray(defaultList);
            }
        }
        return result;
    }

    public boolean insertOrUpdate(String json){
        //保存点检信息
        String taskId = "";
        String checkId = "";
        EqsMaintenanceCheck eqsMaintenanceCheck = JSON.parseObject(json,EqsMaintenanceCheck.class);
        eqsMaintenanceCheck = initEqsMaintenanceCheck_(eqsMaintenanceCheck);
        super.insertOrUpdate(eqsMaintenanceCheck);


        //完成保养任务
//        checkId = eqsMaintenanceCheck.getId();
//        EntityWrapper<EqsMaintenanceTask> entityWrapper = new EntityWrapper<>(EqsMaintenanceTask.class);
//        entityWrapper.eq("checkId",checkId);
//        EqsMaintenanceTask old = eqsMaintenanceTaskService.selectOne(entityWrapper);
//        if(old != null){
//            taskId = old.getId();
//        }
//        EqsMaintenanceTask eqsMaintenanceTask = new EqsMaintenanceTask();
//        eqsMaintenanceTask.setId(taskId);
//        eqsMaintenanceTask.setExecutor(UserUtils.getUser().getRealname());
//        eqsMaintenanceTask.setExecutorId(UserUtils.getUser().getId());
//        eqsMaintenanceTask.setStatus("1");
//        eqsMaintenanceTask.setExecutionTime(new Date());
//        eqsMaintenanceTaskService.insertOrUpdate(eqsMaintenanceTask);
        return true;
    }



    public EqsMaintenanceCheck getById(String json) {
        EqsMaintenanceCheck result = super.getById(json);
        if (result != null) {
            List<FileResponse> defaultList = new ArrayList<FileResponse>();
            if (StringUtils.isNotBlank(result.getCheckAreaImage())) {
                List<FileResponse> responses = JSON.parseArray(result.getCheckAreaImage(), FileResponse.class);
                result.setCheckAreaImageArray(responses);
            } else {
                result.setCheckAreaImageArray(defaultList);
            }

            if (StringUtils.isNotBlank(result.getCheckImportImage())) {
                List<FileResponse> responses = JSON.parseArray(result.getCheckImportImage(), FileResponse.class);
                result.setCheckImportImageArray(responses);
            } else {
                result.setCheckImportImageArray(defaultList);
            }

            if (StringUtils.isNotBlank(result.getUseToolFile())) {
                List<FileResponse> responses = JSON.parseArray(result.getUseToolFile(), FileResponse.class);
                result.setUseToolFileArray(responses);
            } else {
                result.setUseToolFileArray(defaultList);
            }

            if (StringUtils.isNotBlank(result.getCheckStandardFile())) {
                List<FileResponse> responses = JSON.parseArray(result.getCheckStandardFile(), FileResponse.class);
                result.setCheckStandardFileArray(responses);
            } else {
                result.setCheckStandardFileArray(defaultList);
            }

            if (StringUtils.isNotBlank(result.getCheckMethodFile())) {
                List<FileResponse> responses = JSON.parseArray(result.getCheckMethodFile(), FileResponse.class);
                result.setCheckMethodFileArray(responses);
            } else {
                result.setCheckMethodFileArray(defaultList);
            }

            if (StringUtils.isNotBlank(result.getExistRiskFile())) {
                List<FileResponse> responses = JSON.parseArray(result.getExistRiskFile(), FileResponse.class);
                result.setExistRiskFileArray(responses);
            } else {
                result.setExistRiskFileArray(defaultList);
            }

            if (StringUtils.isNotBlank(result.getCountermeasuresFile())) {
                List<FileResponse> responses = JSON.parseArray(result.getCountermeasuresFile(), FileResponse.class);
                result.setCountermeasuresFileArray(responses);
            } else {
                result.setCountermeasuresFileArray(defaultList);
            }
        }
        return result;
    }

    /**
     * 处理数组，转换为字符串
     * @param list
     * @return
     */
    public String getResult_(List<FileResponse> list){
        String result = "";
        if(list != null && list.size() > 0){
            result = JSON.toJSONString(list);
        }
        return result;
    }

    /**
     * 初始化点检bean数据
     * @param eqsMaintenanceCheck
     * @return
     */
    public EqsMaintenanceCheck initEqsMaintenanceCheck_(EqsMaintenanceCheck eqsMaintenanceCheck){
        List<FileResponse> areaImages = eqsMaintenanceCheck.getCheckAreaImageArray();
        List<FileResponse> importImages = eqsMaintenanceCheck.getCheckImportImageArray();
        List<FileResponse> useToolFiles = eqsMaintenanceCheck.getUseToolFileArray();
        List<FileResponse> standardFiles = eqsMaintenanceCheck.getCheckStandardFileArray();
        List<FileResponse> methodFiles = eqsMaintenanceCheck.getCheckMethodFileArray();
        List<FileResponse> existRiskFiles = eqsMaintenanceCheck.getExistRiskFileArray();
        List<FileResponse> countermeasuresFiles = eqsMaintenanceCheck.getCountermeasuresFileArray();
        String areaImage = getResult_(areaImages);
        String importImage = getResult_(importImages);
        String useToolFile = getResult_(useToolFiles);
        String standardFile = getResult_(standardFiles);
        String methodFile = getResult_(methodFiles);
        String existRiskFile = getResult_(existRiskFiles);
        String countermeasuresFile = getResult_(countermeasuresFiles);

        eqsMaintenanceCheck.setCheckAreaImage(areaImage);
        eqsMaintenanceCheck.setCheckImportImage(importImage);
        eqsMaintenanceCheck.setUseToolFile(useToolFile);
        eqsMaintenanceCheck.setCheckStandardFile(standardFile);
        eqsMaintenanceCheck.setCheckMethodFile(methodFile);
        eqsMaintenanceCheck.setExistRiskFile(existRiskFile);
        eqsMaintenanceCheck.setCountermeasuresFile(countermeasuresFile);
        return eqsMaintenanceCheck;
    }
}